import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo Home Page',
      theme: ThemeData(
        primarySwatch: Colors.purple, // 匹配顶部紫色导航栏
      ),
      home: const NotificationDemoPage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

// 鸿蒙通知通信工具类
class HarmonyNotification {
  static const MethodChannel _channel =
      MethodChannel('com.example.work1');
    

  // 请求通知权限
  static Future<bool> requestPermission() async {
    try {
      return await _channel.invokeMethod('requestPermission');
    } on PlatformException catch (e) {
      debugPrint('权限申请失败: ${e.message}');
      return false;
    }
  }

  // 发送通知（接收输入框内容）
  static Future<bool> sendNotification(String content) async {
    try {
      return await _channel.invokeMethod(
        'sendANotification',
        {'content': content},
      );
    } on PlatformException catch (e) {
      debugPrint('通知发送失败: ${e.message}');
      return false;
    }
  }
}

// 核心页面（完全还原图片UI）
class NotificationDemoPage extends StatefulWidget {
  const NotificationDemoPage({super.key});

  @override
  State<NotificationDemoPage> createState() => _NotificationDemoPageState();
}

class _NotificationDemoPageState extends State<NotificationDemoPage> {
  final TextEditingController _contentController = TextEditingController();

  // 发送通知逻辑
  Future<void> _onSend() async {
    final content = _contentController.text.trim();
    if (content.isEmpty) return;

    // 直接发送；插件内已检查并触发授权
    final ok = await HarmonyNotification.sendNotification(content);
    debugPrint('发送结果: $ok');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 顶部紫色导航栏（匹配图片）
      appBar: AppBar(
        backgroundColor: const Color(0xFFE1BEE7), // 淡紫色
        title: const Text(
          'Flutter Demo Home Page',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 40),
        child: Column(
          children: [
            // 通知内容输入框（匹配图片样式）
            TextField(
              controller: _contentController,
              decoration: const InputDecoration(
                labelText: '通知内容',
                labelStyle: TextStyle(fontSize: 14),
                border: OutlineInputBorder(),
                contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 8),
              ),
              maxLength: 8, // 匹配图片中的“0/8”“4/8”字数限制
            ),
            const SizedBox(height: 20),
            // 发送通知按钮（匹配图片样式）
            ElevatedButton(
              onPressed: _onSend,
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.grey[200],
                foregroundColor: Colors.black,
                padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 8),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(4),
                ),
              ),
              child: const Text('发送通知'),
            ),
          ],
        ),
      ),
    );
  }
}